<?php
require_once '../sub/init.php';
header("Cache-control: private");
require_once 'chkUadm.php';
if(!ADMQX('lovetest')){exit(noauth('暂无【情感测试】权限'));}
require_once ZEAI.'cache/config_up.php';
require_once ZEAI.'sub/zeai_up_func.php';
//检查性别库 结束
if ($submitok == 'ajax_addupdate' || $submitok == 'ajax_modupdate'){
	if (empty($title) )json_exit(array('flag'=>0,'msg'=>'请输入题库标题','focus'=>'title'));
	if (str_len($title) >200)json_exit(array('flag'=>0,'msg'=>'亲，题库标题这么长有意义么？ 请不要超过200字节','focus'=>'title'));
	if (str_len($ftitle) >200 || str_len($ftitle) <1)json_exit(array('flag'=>0,'msg'=>'亲，题库副标题长度不能超过200字节且不能为空','focus'=>'ftitle'));
	if (str_len($description) >1000 || str_len($description) <1)json_exit(array('flag'=>0,'msg'=>'亲，题库说明长度不能超过1000字节且不能为空','focus'=>'description'));
	if (str_len($pgbz) >1000 || str_len($pgbz) <1)json_exit(array('flag'=>0,'msg'=>'亲，评估标准长度不能超过1000字节且不能为空','focus'=>'pgbz'));
	if (str_len($pgyj) >1000 || str_len($pgyj) <1)json_exit(array('flag'=>0,'msg'=>'亲，评估依据长度不能超过1000字节且不能为空','focus'=>'pgyj'));
	$title = trimhtml(dataIO($title,'in',200));
	$ftitle = trimhtml(dataIO($ftitle,'in',200));
	$description = dataIO($description,'in',1000);
	$pgbz = dataIO($pgbz,'in',1000);
	$pgyj = dataIO($pgyj,'in',1000);
	$price = floatval($price);
	$jsonstr = stripslashes($jsonstr);
	$jsonstr = dataIO($jsonstr,'in',100000);
}
switch ($submitok){
	case "ajax_addupdate":
		if(!empty($path_s)){
			adm_pic_reTmpDir_send($path_s,'lovetest');
			adm_pic_reTmpDir_send(smb($path_s,'b'),'lovetest');
			$path_s = str_replace('tmp','lovetest',$path_s);
		}
		$db->query("INSERT INTO ".__TBL_LOVETEST_BANK__." (title,ftitle,description,pgbz,pgyj,path_s,price,subjsonstr,px) VALUES ('$title','$ftitle','$description','$pgbz','$pgyj','$path_s','$price','$jsonstr',".ADDTIME.")");
		AddLog('【情感测试】新增题库->【'.$title.'】');
		json_exit(array('flag'=>1,'msg'=>'添加成功'));
	break;
	case "ajax_modupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$row = $db->ROW(__TBL_LOVETEST_BANK__,"path_s"," id=".$id);
		if (!$row)json_exit(array('flag'=>0,'msg'=>'zeai_error_db_id'.$id));$data_path_s= $row[0];
		$path_s = tmp_piclist_modupdate($path_s,$data_path_s,'lovetest','alone');
		$db->query("UPDATE ".__TBL_LOVETEST_BANK__." SET title='$title',ftitle='$ftitle',description='$description',pgbz='$pgbz',pgyj='$pgyj',path_s='$path_s',price='$price',subjsonstr='".$jsonstr."' WHERE id=".$id);
		AddLog('【情感测试】修改题库->【'.$title.'】');
		json_exit(array('flag'=>1,'msg'=>'修改成功'));
	break;
	case"ajax_pic_path_s_up":
		if (ifpostpic($file['tmp_name'])){
			$dbname = setphotodbname('tmp',$file['tmp_name'],'');
			if (!up_send($file,$dbname,0,$_UP['upMsize'],$_UP['upBsize']))json_exit(array('flag'=>0,'msg'=>'图片写入失败'));
			$dbname=setpath_s($dbname);
			$newpic = $_ZEAI['up2']."/".$dbname;
			if (!ifpic($newpic))json_exit(array('flag'=>0,'msg'=>'图片格式错误'));
			json_exit(array('flag'=>1,'msg'=>'上传成功','dbname'=>$dbname));
		}else{
			json_exit(array('flag'=>0,'msg'=>'zeai_tmp_name_error'));
		}
	break;
	case "ajax_delupdate":
		if(!ifint($id))json_exit(array('flag'=>0,'msg'=>'forbidden'));
		$row = $db->ROW(__TBL_LOVETEST_BANK__,"path_s,title"," id=".$id,"name");
		if (!$row)json_exit(array('flag'=>0,'msg'=>'zeai_error_db_id'.$id));
		$path_s   = $row['path_s'];$title = trimhtml(dataIO($row['title'],'out'));
		if(!empty($path_s)){$B = smb($path_s,'b');@up_send_admindel($path_s.'|'.$B);}
		$db->query("DELETE FROM ".__TBL_LOVETEST_QUESTION__." WHERE kind=".$id);
		$db->query("DELETE FROM ".__TBL_LOVETEST_ANSWER__." WHERE qid=".$id);
		$db->query("DELETE FROM ".__TBL_LOVETEST_BANK__." WHERE id=".$id);
		AddLog('【情感测试】删除->【'.$title.'】id:'.$id);
		json_exit(array('flag'=>1,'msg'=>'删除成功'));
	break;
	case"ding":
		if (!ifint($id))alert_adm("forbidden","-1");
		$db->query("UPDATE ".__TBL_LOVETEST_BANK__." SET px=".ADDTIME." WHERE id=".$id);
		header("Location: ".SELF);
	break;
	case"mod":
		$row = $db->ROW(__TBL_LOVETEST_BANK__,"*","id=".$id,"name");
		if ($row){
			$title = dataIO($row['title'],'out');
			$ftitle = dataIO($row['ftitle'],'wx');
			$description = dataIO($row['description'],'wx');
			$pgbz = dataIO($row['pgbz'],'wx');
			$pgyj = dataIO($row['pgyj'],'wx');
			$path_s  = $row['path_s'];
			$price = $row['price'];
			$jsonstr = dataIO($row['subjsonstr'],'out');
		}else{exit('forbidden');}
	break;
}?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<link href="css/main.css?<?php echo $_ZEAI['cache_str'];?>" rel="stylesheet" type="text/css">
<script src="<?php echo HOST;?>/res/www_zeai_cn.js?<?php echo $_ZEAI['cache_str'];?>"></script>
<script>var up2='<?php echo $_ZEAI['up2'];?>/',upMaxMB=<?php echo $_UP['upMaxMB']; ?>;</script>
<style>
#tmp {margin-left:10px}
#tmp input{margin-left:10px;margin-right:10px}
#tmp .tr{margin-bottom:20px}
#tmp textarea{margin-left:10px;height:100px;margin-top:10px;vertical-align:middle}
.jsonlist{border-radius:2px;display:inline-block;background-color:#aaa;padding:2px 7px;margin:3px 10px 3px 0;font-size:12px}
.ed{background-color:#FF5722;}
.jsonlistbox{width:700px;overflow:hidden;display:inline-block;float:left;font-size:12px}
.box{min-width:1300px}
i.add{display:inline-block;color:#fff;border-radius:50px;width:16px;height:16px;line-height:16px;text-align:center;font-size:12px;margin-right:2px}
.picli{padding:0px}
.picli li{width:100px;height:100px;line-height:100px;border:#eee 1px solid;box-sizing:border-box;cursor:pointer;float:left;margin:10px 15px 10px 0;text-align:center;position:relative}
.picli li.add,.picli li i{background-image:url('images/picadd.png');background-size:120px 80px;background-repeat:no-repeat}
.picli li.add{background-size:150px 100px;background-repeat:no-repeat;background-position:-2px -2px;border:#dedede 2px dashed}
.picli li:hover{background-color:#f5f7f9}
.picli li img{vertical-align:middle;margin-top:-5px;max-width:98px;max-height:98px;box-sizing:border-box;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;cursor:zoom-in}
.picli li:hover .img{cursor:zoom-in}
.picli li i{width:20px;height:20px;top:-10px;right:-10px;position:absolute;border-radius:10px;background-position:-80px top;display:block;box-shadow:0 0 5px rgba(0,0,0,0.3)}
.picli li i:hover{background-position:-100px top;cursor:pointer}
.picli #picmore{display:none}
.pathlist img{margin:0 2px 2px 2px;width:30px;height:30px}
i.top{font-size:18px;color:#FF5722}
.ped{color:#FF5722;border-bottom:2px #FF5722 solid;padding-bottom:5px}
</style>
</head>
<body>
<?php 
if($submitok!="add" && $submitok!="mod"){
	$rt = $db->query("SELECT id,title,path_s,price FROM ".__TBL_LOVETEST_BANK__." ORDER BY px DESC,id DESC");
	$total = $db->num_rows($rt);
	$total_str = '<b>'.$total.'</b>';
}
?>
<div class="box">
    <div class="boxL">
        <a href="lovetest_bank.php" class="ed">题库设置<?php echo $total_str;?></a>
        <a href="lovetest_question.php">试题设置</a>
    </div>
    <div class="boxR">
    <?php if($submitok=="add"||$submitok=="mod"){?>
    <table class="table W95_ Mtop20">
        <tr><td height="50" colspan="2" class="S18 B"><?php if($submitok == 'add')echo '创建';if($submitok == 'mod')echo '修改';?>题库</td></tr>
        <form name="ZEAIFORM" id="ZEAIFORM" method="post" enctype="multipart/form-data">
        <tr>
            <td class="tdL">题库标题</td>
            <td class="tdR"><input name="title" type="text" class="W100_ size2" id="title" size="30" maxlength="100" value="<?php echo $title;?>"></td>
        </tr>
        <tr>
            <td class="tdL">题库副标题</td>
            <td class="tdR"><input name="ftitle" type="text" class="W100_ size2" id="ftitle" size="30" maxlength="100" value="<?php echo $ftitle;?>"></td>
        </tr>
        <tr>
            <td class="tdL">题库说明</td>
            <td class="tdR"><textarea id="description" name="description" class="W100_ size1" style="height:80px;"><?php echo $description;?></textarea></td>
        </tr>
        <tr>    
            <td class="tdL">题库主图<br><font class="Cf00 S12">无图前台将不显示</font></td>
            <td class="tdR">
            <div class="picli" id="picli_path">
            <li class="add" id="path_add"></li>
            <?php if(!empty($path_s)){
                echo '<li><img src="'.$_ZEAI['up2'].'/'.$path_s.'"><i></i></li>';
            }?>
            </div>
            <?php if(empty($path_s)){?><br><br><span class="tips" >请配上一张主图</span><?php }?>
            </td>
        </tr>
        <tr>
            <td class="tdL">收费价格</td>
            <td class="tdR"><input id="price" name="price" type="text" class="W100 size2" size="30" value="<?php echo $price;?>"> 元</td>
        </tr>
        <tr>
            <td class="tdL">评估标准</td>
            <td class="tdR"><textarea id="pgbz" name="pgbz" class="W100_ size1" style="height:80px;"><?php echo $pgbz;?></textarea></td>
        </tr>
        <tr>
            <td class="tdL">评估依据</td>
            <td class="tdR"><textarea id="pgyj" name="pgyj" class="W100_ size1" style="height:80px;"><?php echo $pgyj;?></textarea></td>
        </tr>
        <tr>
            <td class="tdL">结果选项</td>
            <td class="tdR S12">
            <div id="tmp">
            <?php
			$a = json_decode($jsonstr);
            if (is_array($a) && count($a)>0){
                for($j=0;$j<count($a);$j++) {
                    $id2    = $a[$j]->s1;
                    $value2 = $a[$j]->v;
                    $score2 = $a[$j]->s2;
					$value2 = dataIO($value2,'wx');
					?>
                    <div class="tr">得分 <input type="text" class="W50 size1" maxlength="10" value="<?php echo $id2;?>">到<input type="text" class="W50 size1" maxlength="10" value="<?php echo $score2;?>"><button type="button" class="btn size1">删除</button><br>答案 <textarea class="W80_ size1"><?php echo $value2;?></textarea></div>
                <?php }
            }
            ?>
            </div>
            <div><button id="add" type="button" class="btn size1 LAN2 ico">&#xe622; 增加</button></div>
            </td>
        </tr>
        <input name="path_s" id="path_s" type="hidden" value="" />
        <input name="submitok" type="hidden" value="ajax_<?php echo $submitok.'update';?>" />
        <?php if ($submitok == 'mod'){?><input name="id" type="hidden" value="<?php echo $id;?>" /><?php }?>
        </form>
    </table>
	<?php
	}else{
		if ($total <= 0 ) {
			if ($submitok !== "add")echo "<div class='nodataico'><i></i>暂无题库<br><a class='btn size2' href='".SELF."?submitok=add'>新增题库</a></div>";
		} else {?>
    <div class="topsearch">
        <button type="button" class="btn tips" onClick="zeai.openurl('<?php echo SELF;?>?submitok=add')" ><i class="ico">&#xe622;</i> 新增题库</button>
    </div>
    <table class="tablelist">
    <tr>
        <th width="60" align="center">ID</th>
        <th width="60" align="center">置顶</th>
        <th width="80" align="left">主图</th>
        <th width="400">题库标题</th>
        <th width="120">试题数量</th>
        <th width="120">收费价格</th>
        <th width="120">已测试人数</th>
        <th align="center">&nbsp;</th>
        <th width="60" class="center">修改</th>
        <th width="60" class="center">删除</th>
        <th width="20" class="center">&nbsp;</th>
    </tr>
    <?php
    for($i=1;$i<=$total;$i++) {
        $rows = $db->fetch_array($rt);
        if(!$rows) break;
        $id = $rows['id'];
        $title = dataIO($rows['title'],'out');
		$path_s    = $rows['path_s'];
        $price = $rows['price'];
        $wznum = $db->COUNT(__TBL_LOVETEST_QUESTION__,"kind=".$id);
		$csrs = $db->COUNT(__TBL_LOVETEST_ANSWER__,"qid=".$id);
		if(!empty($path_s)){
			$path_s_url = $_ZEAI['up2'].'/'.$path_s;
			$path_s_str = '<img src="'.$path_s_url.'">';
		}else{$path_s_url = '';$path_s_str = '';}
		?>
        <tr>
            <td width="60" height="40" align="center"><?php echo $id;?></td>
            <td width="60" height="40" align="center"><a href="<?php echo "lovetest_bank.php?id=".$id; ?>&&submitok=ding" class="topico" title="置顶"></a></td>
            <td width="80" align="left" style="padding:10px 0">
            <?php if (empty($path_s_url)){?>
            <a href="javascript:;" class="pic60 ">无图</a>
            <?php }else{ ?>
            <a href="javascript:;" class="pic60 " onClick="parent.piczoom('<?php echo smb($path_s_url,'b'); ?>');"><?php echo $path_s_str; ?></a>
            <?php }?>
            </td>
            <td width="400"><?php echo $title;?></td>
            <td width="120"><a href="lovetest_question.php?kind=<?php echo $id;?>" class="aHUI"><?php echo($wznum>0)?'<font class="Cf00">'.$wznum.'</font>':0;?></a></td>
            <td width="120"><?php echo ($price>0)?'<font class="Cf00">￥'.$price.'</font>':'<font class="C090">免费</font>';?></td>
            <td width="120"><a class="aHUI" onClick="zeai.iframe('参与测试用户管理','lovetest_u.php?qid=<?php echo $id;?>',700,550)"><?php echo($csrs>0)?'<font class="Cf00">'.$csrs.'</font>':0;?></a></td>
            <td align="center">&nbsp;</td>
            <td width="60" class="center"><a href="<?php echo $SELF;?>?submitok=mod&id=<?php echo $id;?>" class="editbtn" ></a></td>
            <td width="60" class="center"><a value="<?php echo $id; ?>" wznum="<?php echo $wznum;?>" class="delbtn" title="<?php echo $title;?>"></a></td>
            <td width="20" class="center">&nbsp;</td>
          </tr><?php
    }?>
    </table>
    <?php }} ?>
    </div>
    <div class="clear"></div>
</div>
<?php if ($submitok == "add" || $submitok == "mod") {?>
<div class="savebtnbox"><button type="button" id="save" class="btn size3">确定并保存</button></div>
<?php }?>
<script>
<?php if ($submitok == "add" || $submitok == "mod") {?>
	<?php if($submitok=='mod'){?>
    window.onload=function(){
        path_s_mod();
        function path_s_mod(){
            var i=zeai.tag(picli_path,'i')[0],img=zeai.tag(picli_path,'img')[0];
            if(zeai.empty(i))return;
            o('path_add').hide();
            var src=img.src.replace(up2,'');
            path_s.value=src;
            i.onclick = function(){
                zeai.confirm('亲~~确认删除么？',function(){
                    img.parentNode.remove();path_add.show();path_s.value='';
                });
            }
            img.onclick = function(){parent.piczoom(up2+src.replace('_s.','_b.'));}
        }
    }
    <?php }?>
	zeai.photoUp({
		btnobj:path_add,
		upMaxMB:upMaxMB,
		url:"lovetest_bank"+zeai.extname,
		submitok:"ajax_pic_path_s_up",
		end:function(rs){
			zeai.msg(0);zeai.msg(rs.msg);
			if (rs.flag == 1){
				picli_path.append('<li><img src="'+up2+rs.dbname+'"><i></i></li>');
				path_s.value=rs.dbname;
				path_add.hide();
				var i=zeai.tag(o(picli_path),'i')[0],img=zeai.tag(o(picli_path),'img')[0];
				i.onclick = function(){
					zeai.confirm('亲~~确认删除么？',function(){
						img.parentNode.remove();path_add.show();path_s.value='';
					});
				}
				img.onclick = function(){parent.piczoom(up2+rs.dbname.replace('_s.','_b.'));}
			}
		}
	});
	zeai.listEach('.tr',function(obj){
		obj.children[2].onclick=function(){this.parentNode.parentNode.removeChild(this.parentNode);}
	});
	add.onclick = function(){
		var text1 = document.createTextNode('得分 '),text2 = document.createTextNode('答案 '),text3 = document.createTextNode('到'),text4 = document.createElement("br");
		var IDH = zeai.addtag('input');IDH.className = 'W50 size1';IDH.maxLength = 10;
		var Namee = zeai.addtag('textarea');Namee.className = 'W80_ size1';Namee.style.height = '100px';Namee.style.marginTop = '10px';Namee.style.verticalAlign = 'middle';
		var Score = zeai.addtag('input');Score.className = 'W50 size1';Score.maxLength = 10;
		var Btn = zeai.addtag('button');Btn.className = 'btn size1';Btn.html('删除');Btn.onclick=function(){this.parentNode.parentNode.removeChild(this.parentNode);}
		var tr = zeai.addtag('div');tr.className = 'tr';
		tr.appendChild(text1);tr.appendChild(IDH);tr.appendChild(text3);tr.appendChild(Score);tr.appendChild(Btn);tr.appendChild(text4);tr.appendChild(text2);tr.appendChild(Namee);tmp.appendChild(tr);
	}
	save.onclick = function(){
		//子选项
		var idARR = [],jsonarr=[],i=0;
		zeai.listEach('.tr',function(obj){
			var subid   = obj.children[0];
			var subscore = obj.children[1];
			var subname = obj.children[4];
			if (subid.value!=0){if (!zeai.ifint(subid.value)){zeai.msg('请输入起始分值',subid);return false;}}
			if (zeai.empty(subname.value)){zeai.msg('请输入答案',subname);return false;}
			if (subscore.value!=0){if (!zeai.ifint(subscore.value)){zeai.msg('请输入结束分值',subscore);return false;}}
			i += 1;
			idARR.push(i);
			jsonarr.push({"s1":subid.value,"s2":subscore.value,"v":subname.value});
		});
		if (idARR.length <= 0){
			zeai.msg('请点一下【增加】小按钮，谢谢');
			return false;
		}
		var jsonstr = JSON.stringify(jsonarr);
		jsonstr=jsonstr.replace(/\\n/g,"<br>");
		var postjson = {"jsonstr":jsonstr};
		//子选项结束
		zeai.ajax({url:'lovetest_bank'+zeai.extname,data:postjson,form:ZEAIFORM},function(e){var rs=zeai.jsoneval(e);
			if (rs.flag == 1){
				zeai.msg(rs.msg,{time:1});
				setTimeout(function(){zeai.openurl('lovetest_bank.php')},1000);
			}else if(rs.flag == 0){
				parent.zeai.msg(rs.msg,o(rs.focus));
			}else{
				parent.zeai.msg(rs.msg);
			}		
		});
	}
<?php }else{ ?>
	zeai.listEach('.delbtn',function(obj){
		var id = parseInt(obj.getAttribute("value"));
		var wznum = parseInt(obj.getAttribute("wznum"));
		var title = obj.getAttribute("title");
		var tips = (wznum>0)?'当前题库包含 '+wznum+' 道试题':'';
		obj.onclick = function(){
			zeai.confirm('<B class="S18">请慎重，删除后不可恢复</b><br>将删除所有试题和用户测试记录<br>'+tips+'<br>真的要删除【'+title+'】么？',function(){
				zeai.ajax('lovetest_bank'+zeai.ajxext+'submitok=ajax_delupdate&id='+id,function(e){var rs=zeai.jsoneval(e);
					zeai.msg(rs.msg);
					if (rs.flag == 1){setTimeout(function(){location.reload(true);},1000);}
				});
			});
		}
	});
<?php } ?>
</script>
<?php require_once 'bottomadm.php';?>